package juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test01 {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        new Thread(()->{for(int i = 1;i <= 40;i++) ticket.sale();},"A").start();
        new Thread(()->{for(int i = 1;i <= 40;i++) ticket.sale();},"B").start();
        new Thread(()->{for(int i = 1;i <= 40;i++) ticket.sale();},"C").start();
    }
}
class Ticket {
    private int ticket = 30;
    //可重入锁
    Lock lock = new ReentrantLock();

    public void sale() {
        lock.lock();
        try {
            if(ticket > 0) {
                System.out.println(Thread.currentThread().getName()
                        +"\t卖出第：" + (ticket--) +"张票还剩下:" + ticket + "张票");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

}